<?php

class BaseAction extends Action {

	protected $aid;
	protected $power;
	
	protected function _initialize() {	
		//判断是否已登录
		$this->aid = Session::get('admin_id');
		if(empty($this->aid)){
			redirect(__APP__.'/Login');
		}else{
			$Power = D('Admin');
			$this->power = $Power->where('admin_id = '.$this->aid)->getField('power');
		}
		//判断该模块或该操作是否需要超级管理员的权限
		if (array_key_exists(strtolower(MODULE_NAME), C("NEED_AUTH_MODULE"))) {
			$action = C("NEED_AUTH_MODULE." . strtolower(MODULE_NAME));
			if (empty($action) || (is_array($action) && in_array(strtolower(ACTION_NAME), $action))) {
				if(0==$this->power){
					$this->error('权限级别不够!');
				}
			}	
		}
	}
	
	public function init(){
		$data['admin_id'] = Session::get('admin_id');
		$data['logout_time'] = 0;
		$this->action_id = D('Action')->where($data)->getField('action_id');
	}
	/**
	 +------------------------------------------------------------------------------
	 * 公共 文件上传方法
	 * $path    string  上传路径
	 * $maxsize int     上传文件最大值
	 * $thumb   boolean 是否生成缩略图
	 * $width   int     缩略图最大宽度
	 * $height  int     缩略图最大高度
	 * $autosub boolean 是否使用子目录保存文件
	 +------------------------------------------------------------------------------
 	*/
	public function _upload($path,$thumb = false,$width,$height,$autosub = false,$maxsize){
        import("Web.ORG.Net.UploadFile"); 
        $upload = new UploadFile();  
        isset($maxsize) ? $upload->maxSize = $maxsize : $upload->maxSize = 1048576; //1M
		isset($path) ? $upload->savePath = $savepath = "./Attachments/".$path."/" : $upload->savePath = "./Attachments/Others/";
		if(!is_dir($savepath)) @mk_dir($savepath);
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg','txt'); ; 
		if($thumb){
			$upload->thumb = true; 
			$upload->thumbPrefix = '';
			$upload->thumbSuffix = '_thumb';
			isset($wideh) ? $upload->thumbMaxWidth = $width : $upload->thumbMaxWidth = "300"; 
			isset($height) ? $upload->thumbMaxHeight = $height : $upload->thumbMaxHeight = "400"; 
		}
    	if($autosub){
			$upload->autoSub = true;
			$upload->subType = 'date';
        	$upload->saveRule = time; 
			$upload->dateFormat = 'Y/m/d'; 	
		}
        if(!$upload->upload()){  
           	$this->error($upload->getErrorMsg()); 
        }else{ 
			$info = $upload->getUploadFileInfo();
			$info = $info[0]['savename'];
        }
		return $info;
    }
	
	/**
	 +------------------------------------------------------------------------------
	 * 公共 记录管理员操作
	 * 
	 * 
	 +------------------------------------------------------------------------------
	 */
	public function _operationLog(){
		
	}
}
?>
